package AssetCheckSheet

import (
	"ToolLibray/jwtx"
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"property_api/internal/svc"
	"property_api/internal/types"
	"property_api/utils/result"
	"property_api/utils/xform"
	"property_service/property_service"
)

type AddLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddLogic {
	return &AddLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// Add 添加资产盘点单的逻辑
func (l *AddLogic) Add(req *types.AssetCheckSheet) (resp *types.Response, err error) {
	// 设置租户ID，从上下文中获取
	tenantID := jwtx.GetUserTenantId64(l.ctx)
	// 设置创建用户和更新用户，从上下文中获取
	userIdStr := xform.Int64ToString(jwtx.GetUserId(l.ctx))

	// 调用服务层方法，添加资产盘点单记录
	// 服务层方法 AssetCheckSheetAdd 会处理实际的数据库插入操作
	res, err := l.svcCtx.AssetService.AssetCheckSheetAdd(l.ctx, &property_service.AssetCheckSheet{
		CheckCode:     req.CheckCode,     // 盘点单号
		CheckDate:     req.CheckDate,     // 盘点日期
		CheckType:     req.CheckType,     // 盘点类型
		CheckDept:     req.CheckDept,     // 盘点科室
		CheckDeptID:   req.CheckDeptID,   // 盘点科室ID
		CheckPerson:   req.CheckPerson,   // 盘点人
		CheckPersonID: req.CheckPersonID, // 盘点人ID
		Remarks:       req.Remarks,       // 备注
		Count:         req.Count,         // 资产数(行数)
		StatusName:    req.StatusName,    // 状态名称
		StatusID:      req.StatusID,      // 状态ID(关联状态表)
		StockID:       req.StockID,       // 仓库ID
		TenantID:      tenantID,          // 租户ID
		CreatedAtuser: userIdStr,         // 创建用户
		UpdatedAtuser: userIdStr,         // 更新用户
	})

	// 如果服务层返回错误，返回统一的错误响应
	if err != nil {
		return result.Err(err).End()
	}

	resp, err = result.Suc(res).End()
	//获取调用返回的id
	resp.Data = map[string]any{
		"id": res.Id,
	}
	// 返回成功的响应
	return resp, err
}
